
const APP = {
    swithcConfig: [
        { depth: 10, id: 'du6', desc: '首页' },
        { depth: 10, id: 'dua', desc: '热门' },
        { depth: 10, id: 'du9', desc: '发布' },
        { depth: 10, id: 'du8', desc: '消息' },
        { depth: 10, id: 'du7', desc: '我' },
    ],
    //检查存储权限
    hasStoragePermission: function () {
        // 获取应用的PackageManager实例
        var pm = context.getPackageManager();
        // 获取应用的包名
        var packageName = context.getPackageName();
        // 检查权限
        var permission = android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
        var result = pm.checkPermission(permission, packageName);

        // 如果结果等于PackageManager.PERMISSION_GRANTED，则表示有权限
        return result == android.content.pm.PackageManager.PERMISSION_GRANTED;
    },
    //打开应用
    launchApp: function (packageName) {
        // 通过包名打开应用
        var appInfo = context.getPackageManager().getLaunchIntentForPackage(packageName);
        if (appInfo != null) {
            context.startActivity(appInfo);
        } else {
            toast("找不到应用: " + packageName);
        }
    },
    //按坐标点击
    click: function (x, y, delay, optip) {
        click(x, y);
        console.log(optip);
        sleep(delay * 1000);
    },
    //打开个人中心并获取小红书号
    getAccount() {
        id('igz').className('android.widget.TextView').text('我').waitFor();
        if (!id('igz').className('android.widget.TextView').exists()) {
            back();
        }
        if (!id('igz').className('android.widget.TextView').exists()) {
            back();
        }
        App.click(958, 2227, 1, '点击我的，获取小红书号'); //点击我的
        id('gxr').waitFor();
        var text = id('gxr').findOne().text();
        console.log(text);
        //提示草稿箱关闭
        if (className("android.widget.ImageView").depth(6).exists()) {
            className("android.widget.ImageView").click();
        }
    },
    HttpRequest: function () {
        var url = 'https://zfg.isimi.net/app/code/getCodeData';
        var param = {
            a: 1,
            b: 2,
        };

        let response = http.postJson(url, param);
        if (response.statusCode == 200) {
            let jsonData = response.body.json();
            return jsonData;
        } else {
            console.log('请求失败，状态码', response.statusCode);
            return false;
        }
    },
    getTask: function () {
        var jsonData = { type: 'send_post' };

        return jsonData;
    },
    openBtn(index, config) {
        let configBtn = config || this.swithcConfig[index]
        var btn = configBtn.desc ? depth(configBtn.depth).id(configBtn.id).descContains(configBtn.desc).findOne(5000) : depth(configBtn.depth).id(configBtn.id).findOne(5000)
        if (btn) {
            let clickable = btn.click()
            if (!clickable) {
                let space = btn.bounds()
                console.log('space',space);
                
                click(space.centerX(), space.centerY())
            }
        }
    }
};
module.exports = APP